package org.hank.wanandroid.ui

import android.os.Bundle
import android.text.InputType
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.View
import com.google.gson.Gson
import com.lxj.xpopup.impl.ConfirmPopupView
import com.tencent.mmkv.MMKV
import org.hank.commom.base.BaseBindingActivity
import org.hank.commom.utils.KeyBoardUtil
import org.hank.wanandroid.R
import org.hank.wanandroid.constants.Constants
import org.hank.wanandroid.databinding.ActivityLoginBinding
import org.hank.wanandroid.utils.showSnackToast
import org.hank.wanandroid.viewmodel.LoginViewModel

//  ┏┓　　　┏┓
//┏┛┻━━━┛┻┓
//┃　　　　　　　┃
//┃　　　━　　　┃
//┃　┳┛　┗┳　┃
//┃　　　　　　　┃
//┃　　　┻　　　┃
//┃　　　　　　　┃
//┗━┓　　　┏━┛
//    ┃　　　┃                  神兽保佑
//    ┃　　　┃                  永无BUG！
//    ┃　　　┗━━━┓
//    ┃　　　　　　　┣┓
//    ┃　　　　　　　┏┛
//    ┗┓┓┏━┳┓┏┛
//      ┃┫┫　┃┫┫
//      ┗┻┛　┗┻┛
/**
 *@auth: Hank
 *邮箱: cs16xiaoc1@163.com
 *创建时间: 2021/4/22 11:30
 *描述:
 */
class LoginActivity : BaseBindingActivity<ActivityLoginBinding>() {
    override val layoutResId: Int
        get() = R.layout.activity_login

    private var isPasswordShow = false
    lateinit var xPopupp: ConfirmPopupView

    private val loginViewModel by getViewModel(LoginViewModel::class.java) {
        loginResult.observe(this@LoginActivity) {
            if (it != null) {
                MMKV.defaultMMKV()?.putString(Constants.LOGIN_INFO, Gson().toJson(it))
                MMKV.defaultMMKV()?.putBoolean(Constants.LOGIN_STATUS, true)
                finish()
            }
        }
        finallyLive.observe(this@LoginActivity) {
            if (it)
                setViewStatus(it)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        dataBinding.item = loginViewModel
        dataBinding.loginHelper = LoginHelper()
    }

    inner class LoginHelper {
        fun login() {
            //判断账户密码是否为空
            KeyBoardUtil.closeKeyboard(dataBinding.etUsername.windowToken,this@LoginActivity)
            if (loginViewModel.userName.value.isNullOrEmpty()) {
                showSnackToast("用户名不能为空")
                return
            }
            if (loginViewModel.passWord.value.isNullOrEmpty()) {
                showSnackToast("密码不能为空")
                return
            }
            setViewStatus(false)
            loginViewModel.login()
        }

        fun finish() {
            this@LoginActivity.finish()
        }

        fun clearUserName() {
            loginViewModel.userName.value = ""
        }

        fun showAndHidePass() {
            if (dataBinding.etPassword.inputType == InputType.TYPE_TEXT_VARIATION_PASSWORD)
                InputType.TYPE_CLASS_TEXT
            else
                InputType.TYPE_TEXT_VARIATION_PASSWORD

            dataBinding.etPassword.transformationMethod = if (isPasswordShow) {
                isPasswordShow = false
                dataBinding.ivPasswordVisibility.setImageResource(R.mipmap.password_hide)
                PasswordTransformationMethod.getInstance()
            } else {
                isPasswordShow = true
                dataBinding.ivPasswordVisibility.setImageResource(R.mipmap.password_show)
                HideReturnsTransformationMethod.getInstance()
            }
            dataBinding.etPassword.setSelection(dataBinding.etPassword.text.length)
        }
    }

    /**
     * 登录时给具备点击事件的View上锁，登陆失败时解锁
     * 并且施加动画
     */
    private fun setViewStatus(lockStatus: Boolean) {
        dataBinding.apply {
            llLogin.isEnabled = lockStatus
            tvRegister.isEnabled = lockStatus
            tvSkip.isEnabled = lockStatus
            etUsername.isEnabled = lockStatus
            etPassword.isEnabled = lockStatus
            if (lockStatus) {
                tvLoginTxt.visibility = View.VISIBLE
                indicatorView.visibility = View.GONE
                indicatorView.hide()
            } else {
                tvLoginTxt.visibility = View.GONE
                indicatorView.visibility = View.VISIBLE
                indicatorView.show()
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        dataBinding.unbind()
    }
}

